package com.example.demo.service;


import com.example.demo.client.SmQryClient;
import com.example.demo.client.evt.sysbasequery.QueryLogEvt;
import com.example.demo.client.evt.sysbasequery.QuerySystemConfigEvt;
import com.example.demo.domain.SystemConfig;
import com.example.demo.mapper.LogMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Desc
 * @Author zhengmf
 * @Date 2020-03-30 16:04:35
 */
@Component
public class ClearLogJob {
    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LogMapper logMapper;
    @Autowired
    private SmQryClient smQryClient;
    @Autowired
    private LogService logService;
    @Autowired
    private SystemConfigService systemConfigService;


    @Scheduled(cron = "0 0 2 * * ?")
    private void configureTasks() {
        QuerySystemConfigEvt evt = new QuerySystemConfigEvt();
        evt.setKey("LOG_BACKUP_LENGTH");
        int backupMonth = Integer.parseInt((smQryClient.querySystemConfig(evt).getBody().getRecords()!=null?smQryClient.querySystemConfig(evt).getBody().getRecords().get(0).getValue():"1"));
        Date date = new Date();
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(date.getMonth()-backupMonth<=0)
        {
            int num = (backupMonth - date.getMonth())/12;
            date.setMonth(date.getMonth() - backupMonth +12*num);
            date.setYear(date.getYear()-num);
        }else {
            date.setMonth(date.getMonth() - backupMonth);
        }
        String timeLine = df.format(returnZero(date));

        //删除前查一下前一天的日志并备份
        QueryLogEvt queryLogEvt = new QueryLogEvt();
        date = new Date();
        queryLogEvt.setEndTime(df.format(returnZero(date)));
        date.setDate(date.getDate()-1);
        queryLogEvt.setStartTime(df.format(returnZero(date)));
        logService.exportLogAuto(queryLogEvt);

        //更新备份时间
        evt.setKey("LOG_BACKUP_DAY");
        Long id = smQryClient.querySystemConfig(evt).getBody().getRecords()!=null?smQryClient.querySystemConfig(evt).getBody().getRecords().get(0).getId():99L;
        SystemConfig logBackupDay = new SystemConfig();
        logBackupDay.setId(id);
        logBackupDay.setValue(df.format(new Date()));
        logBackupDay.setKey("LOG_BACKUP_DAY");
        logBackupDay.setDesc("日志备份时间");
        logBackupDay.setStatus("E");
        systemConfigService.saveSysConfig(logBackupDay);
        smQryClient.clearSysConfigCache();

        //清除设置范围前的日志数据
        try {
            LOGGER.info("清理日志数据");
            logMapper.clearLogBefore30day(timeLine);
        }catch (Exception e){
            LOGGER.info("清理日志失败");
        }

    }

    private Date returnZero(Date date){
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        return date;
    }
}
